#pragma once

void gotoxy(int x, int y)
{
	COORD point = {x, y};
	SetConsoleCursorPosition(
		GetStdHandle(STD_OUTPUT_HANDLE), point);
}

struct Map
{
	//Two dimensional array
	char ** map;
	int width, height;	//size of 2d array
	int posX, posY;			//The x/y coordinate of the map.
	//returns false if could not load from file

//Constructor
	Map()
	{
		map = NULL;
		width = 0;
		height = 0;
		posX = 0; 
		posY = 0;
	}
//Deconstructor
	~Map()
	{
		if (map)
		{
			for (int i = 0; i < height; i++)
			{
				delete [] map[i];
			}
			delete [] map;
		}
		map = NULL;
	}


	bool loadFile(char * filename)
	{
		ifstream fin(filename);
		if (!fin.is_open())
		{
			return false;
		}
		fin >> width;
		fin >> height;
#ifdef DEBUG
		printf("This map is %d x %d\n", width, height);
#endif
		map = new char * [height];
		for (int i = 0; i < height; i++)
		{
			map[i] = new char [width];
		}
		char input;
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				input = fin.get();
				if (input == '\n' || input == '\r')
				{
					--x;
				}
				else
				{
					map[y][x] = input;
				}
			}
		}
#ifdef DEBUG		//Debug code (like a boss)
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				putchar(map[y][x]);
			}
			putchar('\n');
		}
#endif
		fin.close();
		return true;
	}
	void draw()
	{
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				gotoxy(posX+x, posY+y);
				putchar(map[y][x]);
			}
			putchar('\n');
		}
	}
	void draw(int a_x, int a_y)
	{
		
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[y][x]);
			}
		}
	}


//Draw flipped/rotated functions
	void drawFlippedH(int a_x, int a_y)
	{
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[y][(width - 1) - x]);
			}
		}
	}
	void drawFlippedV(int a_x, int a_y)
	{	
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[(height-1)-y][x]);
			}
		}
	}
	void drawFlippedXY(int a_x, int a_y)
	{
		for (int x = 0; x < height; ++x)
		{
			for (int y = 0; y < width; ++y)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[x][y]);
			}
		}
	}
	void drawCW(int a_x, int a_y)
	{
		for (int x = 0; x < height; ++x)
		{
			for (int y = 0; y < width; ++y)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[(height-1)-x][y]);
			}
		}
	}
	void drawCCW(int a_x, int a_y)
	{
		for (int x = 0; x < height; ++x)
		{
			for (int y = 0; y < width; ++y)
			{
				gotoxy(a_x+x, a_y+y);
				putchar(map[x][(width-1)-y]);
			}
		}
	}
//End draw flipped/rotated functions

//Start modify flipped/rotated functions
	void flipH()
	{
		//Get some new memory
		char ** temp = new char * [height];
		for (int i = 0; i < height; i++)		
		{
			temp[i] = new char[width];
		}
		//Transfer the data over to the new memory
		for (int y = 0; y < height; y++)
		{
			for (int x = 0; x < width; x++)
			{
				temp[y][x] = map[y][(width-1)-x];
			}
		}
		//kill the old data
		for(int i = 0; i < height; i++)
		{
			delete [] map[i];
		}
		delete [] map;
		//use the new memory
		map = temp;
		temp = NULL;
	}

	void flipV()
	{
		//Get some new memory
		char ** temp = new char * [height];
		for (int i = 0; i < height; i++)		
		{
			temp[i] = new char[width];
		}
		//Transfer the data over to the new memory
		for (int y = 0; y < height; y++)
		{
			for (int x = 0; x < width; x++)
			{
				temp[y][x] = map[(height-1)-y][x];
			}
		}
		//kill the old data
		for(int i = 0; i < height; i++)
		{
			delete [] map[i];
		}
		delete [] map;
		//use the new memory
		map = temp;
		temp = NULL;
	}
	void flipXY()
	{
		//make the new memory
		char ** temp = new char * [width];
		for (int i = 0; i < width; ++i)
		{
			temp[i] = new char[height];
		}
		//Set the new memory
		for (int y = 0; y < height; ++y)
		{
			for (int x = 0; x < width; ++x)
			{
				temp[x][y] = map[y][x];
			}
		}
		//Kill the old data
		for (int i = 0; i < height; ++i)
		{
				delete [] map[i];
		}
		delete [] map;
		map = temp;
		temp = NULL;
		//Height is now swapped with width.
		int t = height;
		height = width;
		width = t;
	}

	void rotateCCW()
	{
		flipXY();
		flipV();
	}

	void rotateCW()
	{
		flipXY();
		flipH();
	}
//End modify flipped/rotated functions
};